package com.bxtel.bo;

import java.util.HashMap;
import javax.transaction.Transactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.repository.CrudRepository;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.stereotype.Service;

import com.bxtel.dao.SmsRepository;
import com.bxtel.model.Sms;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import dinamica.util.DateHelper;
import dinamica.util.HttpHelper;
import dinamica.util.JsonHelper;


@Service
public class SmsBO {
	
	
	public static void main(String[] args) throws JsonProcessingException {
		HashMap param = new HashMap();
		param.put("username", "");
		param.put("password", "");
		param.put("Ext", "");
		//param.put("passwordMd5", "");
		param.put("mobile", "13881757831");//
		param.put("message", "【返利宝会员管家】440746(返利宝验证码),为了安全起见,验证短信请勿转发他人");
		HashMap bbb = HttpHelper.sendPost("http://211.151.85.133:8080/sendsms.asp",param , "GBK");
		System.out.println(JsonHelper.getObjectMapperInstance().writeValueAsString(bbb));
	}
	
	
	@Autowired
	SmsRepository  repository;
	
	
	@JmsListener(destination = "smsQueue")
    public void processMessage(String content) throws Exception {
		ObjectMapper om=new ObjectMapper();
		try {
			Sms sms=om.readValue(content, Sms.class);
			send(sms);
		}catch (Exception e) {
			e.printStackTrace();
			throw e;
		}
    }
	
	
	@Transactional
	public void send(Sms sms) throws Exception {
		try {
			HashMap param = new HashMap();
			param.put("username", "");
			param.put("password", "");
			param.put("Ext", "");
			//param.put("passwordMd5", "");
			param.put("mobile", sms.getMobile());
			param.put("message","【返利宝会员管家】"+sms.getContent());
			HashMap bbb = HttpHelper.sendPost("http://211.151.85.133:8080/sendsms.asp",param , "GBK");
			System.out.println(JsonHelper.getObjectMapperInstance().writeValueAsString(bbb));
			if(bbb.get("returncode").equals("00000000"))
			{
				String msgid=(String)bbb.get("data");
				if(new Integer(msgid)>0)
				{
					sms.setMsgid(msgid);
					sms.setStatus("1");
					repository.save(sms);
				}
				else
				{
					throw new Exception((String) errorMap.get(msgid));
				}
			}else{
				throw new Exception((String)bbb.get("returnmsg"));
			}
		}catch (Exception e) {
			e.printStackTrace();
			throw e;
		}
    }
	
	public void updateStatus(String msgid,String status)
	{
		Sms sms=repository.findByMsgid(msgid);
		sms.setStatus(status);
		repository.save(sms);
	}
	
	
	
	static HashMap errorMap=new HashMap();
	static {
		errorMap.put("-1", "账号或者密码错误");
		errorMap.put("-2", "缺少企业账号");
		errorMap.put("-3", "缺少密码");
		errorMap.put("-4", "缺少短信内容");
		errorMap.put("-5", "缺少目标号码");
		errorMap.put("-7", "短信内容过长(小灵通最大56个字)");
		errorMap.put("-8", "含有非法字符，第二行返回非法关键词");
		errorMap.put("-9", "目标号码格式错误，或者包含错误的手机号码");
		errorMap.put("-10", "超过规定发送时间，禁止提交发送");
		errorMap.put("-12", "余额不足");
		errorMap.put("-14", "号码超过发送数量限制");
		errorMap.put("-15", "发送内容前面需加签名，例如【XXX公司】，签名必须放在最前面，如果内容的编码错误，出现乱码，识别不出签名【XXX公司】，也会返回-15");
		errorMap.put("-16", "提交号码数量小于最小提交量限制");
		errorMap.put("-20", "未开通接口");
		errorMap.put("-22", "短信内容签名不正确");
		errorMap.put("-99", "连接失败");
		errorMap.put("-100", "系统内部错误");
	}
}